package cn.thoughtworks.school.programCenter.controllers;

import cn.thoughtworks.school.programCenter.annotations.Auth;
import cn.thoughtworks.school.programCenter.entities.QuizSuggestion;
import cn.thoughtworks.school.programCenter.entities.User;
import cn.thoughtworks.school.programCenter.exceptions.BusinessException;
import cn.thoughtworks.school.programCenter.repositories.QuizSuggestionRepository;
import cn.thoughtworks.school.programCenter.services.AssignmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/api/v2")
public class QuizSuggestionController {
    @Autowired
    private QuizSuggestionRepository quizSuggestionRepository;
    @Autowired
    private AssignmentService assignmentService;

    @PostMapping(value = "/suggestions")
    public ResponseEntity addSuggestionToQuiz(@RequestBody QuizSuggestion suggestion, @Auth User current) {
        suggestion.setCreateTime(new Date());
        suggestion.setFromUserId(current.getId());
        suggestion = quizSuggestionRepository.save(suggestion);

        Map result = new HashMap<>();
        result.put("id", suggestion.getId());

        return new ResponseEntity<>(result, HttpStatus.CREATED);
    }

    @GetMapping(value = "/students/{studentId}/assignments/{assignmentId}/quizzes/{quizId}/suggestions")
    public ResponseEntity getSuggestionsByAssignmentsAndQuizzes(@PathVariable Long studentId,
                                                                @PathVariable Long assignmentId,
                                                                @PathVariable Long quizId) {
        List<QuizSuggestion> foundSuggestions = assignmentService.getQuizSuggestions(studentId, assignmentId, quizId);
        return new ResponseEntity<>(foundSuggestions, HttpStatus.OK);
    }

    @GetMapping(value = "/assignments/{assignmentId}/quizzes/{quizId}/suggestions")
    public ResponseEntity studentGetSuggestionsByStudentIdAndAssignmentsAndQuizzes(@Auth User user, @PathVariable Long assignmentId, @PathVariable Long quizId) {
        List<QuizSuggestion> foundSuggestions = assignmentService.getQuizSuggestions(user.getId(), assignmentId, quizId);

        return new ResponseEntity<>(foundSuggestions, HttpStatus.OK);
    }

    @RequestMapping(value = "/suggestions/{id}", method = RequestMethod.PUT)
    public ResponseEntity updateSuggestion(@PathVariable Long id, @RequestBody Map date) throws BusinessException {
        QuizSuggestion suggestion = quizSuggestionRepository
                .findById(id)
                .orElseThrow(() -> new BusinessException("该评论不存在"));

        suggestion.setContent((String) date.get("content"));
        quizSuggestionRepository.save(suggestion);

        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }

    @RequestMapping(value = "/suggestions/{id}", method = RequestMethod.DELETE)
    public ResponseEntity deleteSuggestion(@PathVariable Long id) throws BusinessException {
        QuizSuggestion suggestion = quizSuggestionRepository
                .findById(id)
                .orElseThrow(() -> new BusinessException("该评论不存在"));

        quizSuggestionRepository.deleteById(id);

        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }
}
